﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JCPogi.Core.Helpers;

namespace JCPogi.Core {
    
    public class SendBulkSmsCommandContext : DeviceCommandContext {        

        public SendBulkSmsCommandContext(IEnumerable<string> recipientList, string message, 
                                         CommunicationDevice communicationDevice)
            : base(communicationDevice) {
                
            _recipientList = recipientList;
            _message = message;
        }

        public override DeviceCommandResult PerformCommand(PerformCommandContext context) {
            foreach (var recipient in _recipientList) {
                var cmd = new SendSmsCommandContext(recipient, _message, Device);
                var result = cmd.PerformCommand(context);
                if (!result.IsSuccessful) {
                    context.OnPublishUpdate("FAILED: {0}".WithTokens(recipient));
                }
                else {
                    context.OnPublishUpdate("SENT: {0}".WithTokens(recipient));
                }
            }

            return DeviceCommandResult.Successful;
        }

        private readonly IEnumerable<string> _recipientList;
        private readonly string _message;
    }
}
